<template>
  <div>
    <van-nav-bar
      title="修改地址"
      left-arrow
      fixed
      placeholder
      @click-left="onClickLeft"
    />
    <!-- 添加地址 -->
    <van-address-edit
      :area-list="areaList"
      show-delete
      :address-info="addrinfo"
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
    />
  </div>
</template>

<script>
// vant 省市区数据
import { areaList } from "@vant/area-data";
import { Post } from "../gp";
import { Delete } from "../gp";
export default {
  data() {
    return {
      areaList,
      searchResult: [],
      addrinfo: {
        id: this.$route.params.id,
        name: this.$route.params.name,
        tel: this.$route.params.tel,
        areaCode: this.$route.params.areaCode,
        province: this.$route.params.province,
        city: this.$route.params.city,
        county: this.$route.params.area,
        addressDetail: this.$route.params.addressDetail,
      },
    };
  },
  mounted() {
    console.log(this.$route.params);
  },
  methods: {
    onClickLeft() {
      //  返回地址管理
      this.$router.go(-1);
    },
    onSave(content) {
      console.log(content);
      let data = {};
      data.user_id = this.$route.params.id;
      data.receiver = content.name;
      data.province = content.province;
      data.city = content.city;
      data.area = content.county;
      data.areacode = content.araeCode;
      data.addr = content.addressDetail;
      data.cellphone = content.tel;
      data.is_default = content.isDefault==false?0:1;
      Post("addr/add", data).then((res) => {
        console.log(res);
        console.log("添加成功");
        if (res.data.code == 200) {
          this.$router.push("/address");
        }
      });
    },
    onDelete() {
      let data = {};
      Delete(`/addr/delete/${this.$route.params.id}`, data).then((result) => {
        console.log(result);
        if (result.data.code == 200) {
          this.$Toast.success("删除成功");
          this.$router.push("/address");
        } else {
          this.$Toast.fail("删除失败");
        }
      });
    },
  },
};
</script>

<style lang="scss">
//顶部返回箭头样式
.van-nav-bar__placeholder > div > div > .van-nav-bar__left {
  i {
    color: black;
  }
}
</style>